home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 May / EnigmA AMIGA RUN 18 (1997)(G.R. Edizioni)(IT)[!][issue 1997-05][EAR-CD II].iso / earcd / dev / c / assignp.lha / AssignP / AssignP.c < prev    next >
C/C++ Source or Header  |  1997-01-23  |  10KB  |  327 lines

  1. /************************************************************************
  2.  *                                                                      *
  3.  *  AssignP - trap easyrequests for non existant device or volumes      *
  4.  *                                                                      *
  5.  *  (originally based on AssignX by Steve Tibbet, but has been          *
  6.  *   completely rewritten several times)                                *
  7.  *                                                                      *
  8.  *  All changes made: put into the Public Domain.                       *
  9.  *                                                                      *
  10.  *  rewritten and enhanced by Andreas R. Kleinert                       *
  11.  *  Andreas_Kleinert@t-online.de                                        *
  12.  *                                                                      *
  13.  *  No Assembler - 100 percent C                                        *
  14.  *                                                                      *
  15.  * V1.03 (23.1.97):                                                     *
  16.  *                 - fixed ASL problem                                  *
  17.  *                 - now background tool                                *
  18.  *                 - using SAS/C V6.57                                  *
  19.  *                 - renamed to AssignP                                 *
  20.  *                 - etc.                                               *
  21.  *                                                                      *
  22.  * V1.02 :                                                              *
  23.  *                 - private version                                    *
  24.  *                 - using SAS/C V6.55                                  *
  25.  *                 - needs 3.1 (V40) now                                *
  26.  *                 - and more                                           *
  27.  *                                                                      *
  28.  * V1.01 :                                                              *
  29.  *                 - renamed to "DAssignX"                              *
  30.  *                 - made necessary changes to run it on a              *
  31.  *                   3.0-locale-WB                                      *
  32.  *                 - used SAS/C V6.00                                   *
  33.  *                                                                      *
  34.  ************************************************************************/
  35.  
  36. #define __USE_SYSBASE
  37.  
  38. #include <exec/memory.h>
  39. #include <intuition/intuitionbase.h>
  40. #include <libraries/gadtools.h>
  41.  
  42. #include <string.h>
  43.  
  44. #include <proto/exec.h>
  45. #include <proto/dos.h>
  46. #include <proto/intuition.h>
  47. #include <proto/asl.h>
  48.  
  49. #define N (NULL)
  50.  
  51. struct AbortedList
  52. {
  53.  struct AbortedList *Next;
  54.  char Name[256];
  55. };
  56.  
  57. struct IntuitionBase *IntuitionBase   = N;
  58. struct Library       *AslBase         = N;
  59. struct Remember      *Remember        = N;
  60. struct AbortedList   *AbortedRequests = N;
  61.  
  62. char __aligned *checktext [] =
  63. {
  64.  "Benötige den Datenträger",
  65.  "Please insert volume",
  66.  ""
  67. };
  68.  
  69.  
  70. APTR __far NewEasyVec;
  71. long __far __asm (*OrigEasyVec)(register __a0 struct Window        *Win_a0,
  72.                                 register __a1 struct EasyStruct    *EZ_a1,
  73.                                 register __a2 ULONG                *idcmp,
  74.                                 register __a3 ULONG                *args,
  75.                                 register __a6 struct IntuitionBase *IntuitionBase);
  76.  
  77.  
  78. ULONG __saveds __asm NewEasy(register __a0 struct Window     *Win_a0,
  79.                              register __a1 struct EasyStruct *EZ_a1,
  80.                              register __a2 ULONG             *idcmp,
  81.                              register __a3 ULONG             *args,
  82.                              register __a6 struct IntuitionBase *IntuitionBase_a6)
  83. {
  84.  struct Window     *Win = Win_a0;
  85.  struct EasyStruct *EZ  = EZ_a1;
  86.  ULONG retval = 0, catch = 0, case_true = FALSE, i, found = 0;
  87.  UBYTE *oldstr = N;
  88.  struct IntuitionBase *IntuitionBase = IntuitionBase_a6;
  89.  
  90.  
  91.  for(i=0; checktext[i][0]; i++)
  92.   {
  93.    if(args)
  94.     {
  95.      if(!strncmp(  (char *)args[0], checktext[i], strlen(checktext[i]))) { case_true = TRUE; found = i; }
  96.     }
  97.  
  98.    if(EZ)
  99.     {
  100.      if(!strncmp(EZ->es_TextFormat, checktext[i], strlen(checktext[i]))) { case_true = TRUE; found = i; }
  101.     }
  102.   }
  103.  
  104.  if(case_true)
  105.   {
  106.    struct AbortedList *AL=AbortedRequests;
  107.  
  108.    while (AL)
  109.     {
  110.      if (stricmp((char *)AL->Name, (char *)args[1])==0) return(0);
  111.  
  112.      AL=AL->Next;
  113.     }
  114.  
  115.    oldstr = EZ->es_GadgetFormat;
  116.  
  117.    EZ->es_GadgetFormat = "Try again|Mount|Assign|Cancel Forever|Cancel";
  118.  
  119.    catch=1;
  120.   }
  121.  
  122.  retval = OrigEasyVec(Win, EZ, idcmp, args, IntuitionBase);
  123.  
  124.  if(catch)
  125.   {
  126.    EZ->es_GadgetFormat = oldstr;
  127.  
  128.    switch(retval)
  129.     {
  130.      case 2:
  131.             {
  132.              UBYTE *strbuffer;
  133.  
  134.  
  135.              retval = 0; /* until success */
  136.  
  137.              strbuffer = (UBYTE *) AllocVec(512, MEMF_CLEAR|MEMF_PUBLIC);
  138.              if(strbuffer)
  139.               {
  140.                strcpy(strbuffer, "C:Mount ");
  141.                strcat(strbuffer, (char *) args[1]);
  142.  
  143.                SystemTagList(strbuffer, N);
  144.  
  145.                retval = 1;
  146.  
  147.                FreeVec(strbuffer);
  148.               }
  149.  
  150.              break;
  151.             }
  152.      case 3:
  153.             {
  154.              UBYTE *strbuffer;
  155.  
  156.  
  157.              retval = 0; /* until success */
  158.  
  159.              strbuffer = (UBYTE *) AllocVec(1024, MEMF_CLEAR|MEMF_PUBLIC);
  160.              if(strbuffer)
  161.               {
  162.                UBYTE *buff, *FullName;
  163.                struct FileRequester *request;
  164.                struct TagItem __aligned tags[4];
  165.  
  166.  
  167.                buff     = &strbuffer[0];
  168.                FullName = &strbuffer[512];
  169.  
  170.                strcpy(buff, "AssignP: Assign for '");
  171.                strcat(buff, (char *)args[1]);
  172.                strcat(buff, "':");
  173.  
  174.                tags[0].ti_Tag  = (Tag)   ASL_Hail;
  175.                tags[0].ti_Data = (ULONG) buff;
  176.  
  177.                tags[1].ti_Tag  = (Tag)   ASL_OKText;
  178.                tags[1].ti_Data = (ULONG) "Assign";
  179.  
  180.                tags[2].ti_Tag  = (Tag)   ASL_CancelText;
  181.                tags[2].ti_Data = (ULONG) "Cancel";
  182.  
  183.                tags[3].ti_Tag  = (Tag)   TAG_DONE;
  184.                tags[3].ti_Data = (ULONG) N;
  185.  
  186.                request = AllocAslRequest(ASL_FileRequest, N);
  187.                if(request)
  188.                 {
  189.                  if(AslRequest(request, &tags[0]))
  190.                   {
  191.                    ULONG len;
  192.                    BPTR  lock;
  193.  
  194.                    strcpy(FullName, request->rf_Dir);
  195.  
  196.                    len = strlen(FullName);
  197.                    if(len)
  198.                     {
  199.                      len--;
  200.  
  201.                      if(   (FullName[len] != ':')
  202.                          &&(FullName[len] != '/')
  203.                          &&(FullName[len] != ' ') ) strcat(FullName, "/");
  204.                     }
  205.  
  206.                    strcat(FullName, request->rf_File);
  207.  
  208.                    lock=Lock(FullName, ACCESS_READ);
  209.                    if(lock)
  210.                     {
  211.                      retval = 1;
  212.                      AssignLock((char *)args[1], lock);
  213.                     }
  214.                   }
  215.                  FreeAslRequest(request);
  216.                 }
  217.  
  218.                FreeVec(strbuffer);
  219.               }
  220.  
  221.              break;
  222.             }
  223.      case 4:
  224.             {
  225.              struct AbortedList *AL;
  226.  
  227.              AL = (struct AbortedList *) AllocRemember(&Remember, strlen((char *)args[1])+sizeof(struct AbortedList)+2, MEMF_CLEAR|MEMF_PUBLIC);
  228.              if(AL)
  229.               {
  230.                strcpy(AL->Name, (char *)args[1]);
  231.  
  232.                AL->Next=AbortedRequests;
  233.                AbortedRequests=AL;
  234.               }
  235.  
  236.              break;
  237.             }
  238.         }
  239.   }
  240.  
  241.  return(retval);
  242. }
  243.  
  244. /* *************************************************** */
  245. /* *                                                 * */
  246. /* * Compiler Stuff for BackgroundIO                 * */
  247. /* *                                                 * */
  248. /* *************************************************** */
  249.  
  250. long  __stack        = 2048;
  251. char *__procname     = "AssignP";
  252. long  __priority     = 1;
  253. long  __BackGroundIO = 1;           /* TRUE : We DO BackGroundIO !     */
  254.  
  255. extern BPTR _Backstdout;            /* NULL, if started from Workbench */
  256.  
  257. void __regargs __chkabort(void) { }
  258. void __regargs _CXBRK(void)     { }
  259.  
  260. char vertext [] = "\0$VER: AssignP V1.03 (private)";
  261.  
  262.  
  263. typedef unsigned long (*FUNCCAST)();
  264.  
  265. long main(long argc, char **argv)
  266. {
  267.  long retval = 0;
  268.  
  269.  APTR task;
  270.  
  271.  Forbid();
  272.  task = FindTask(N);
  273.  Permit();
  274.  
  275.  if(task) SetTaskPri(task, __priority);
  276.  
  277.  
  278.  IntuitionBase = (APTR) OpenLibrary("intuition.library", 40);
  279.  if(IntuitionBase)
  280.   {
  281.    AslBase = (APTR) OpenLibrary("asl.library", 40);
  282.    if(AslBase)
  283.     {
  284.      OrigEasyVec = (long (* __asm )(register __a0 struct Window        *Win_a0,
  285.                                     register __a1 struct EasyStruct    *EZ_a1,
  286.                                     register __a2 ULONG                *idcmp,
  287.                                     register __a3 ULONG                *args,
  288.                                     register __a6 struct IntuitionBase *IntuitionBase)) SetFunction((APTR)IntuitionBase, -588, (APTR)NewEasy);
  289.      if(OrigEasyVec)
  290.       {
  291.        if(task) SetTaskPri(task, -10);
  292.        Wait(SIGBREAKF_CTRL_C);
  293.        if(task) SetTaskPri(task, 1);
  294.  
  295.        NewEasyVec= (APTR) SetFunction((APTR)IntuitionBase, -588, (APTR)OrigEasyVec);
  296.  
  297.        if(NewEasyVec!=NewEasy)
  298.         {
  299.          ULONG idcmp = N;
  300.          struct EasyStruct estr;
  301.  
  302.          estr.es_StructSize   = sizeof(struct EasyStruct);
  303.          estr.es_Flags        = N;
  304.          estr.es_Title        = "AssignP Request";
  305.          estr.es_TextFormat   = "Error while removing from system !\n"
  306.                                 "There were more patches !"
  307.                                 "Do a reset soon !";
  308.          estr.es_GadgetFormat = "Ok";
  309.  
  310.          EasyRequestArgs(N, &estr, (ULONG *) &idcmp, N);
  311.         }
  312.  
  313.        FreeRemember(&Remember, TRUE);
  314.  
  315.       }else retval = 20;
  316.  
  317.      CloseLibrary(AslBase);
  318.  
  319.     }else retval = 20;
  320.  
  321.    CloseLibrary(IntuitionBase);
  322.  
  323.   }else retval = 20;
  324.  
  325.  return(retval);
  326. }
  327.